<?php

/**
 * Class Involved_ShopProductModel
 */

class Involved_ShopProductModel extends BaseModel{

    protected $table = 'shop_product';

    /**
     * 商品列表
     * @return mixed
     */
    public function YptShopList()
    {
        return  DB::table($this->table)
            ->select('product_id','product_name','shop_product.price','category.cate_name','shop_brand.brand_name','specificate.spec_name','business_name','stock','shop_product.created','shop_product.is_show','img1','place','shop_product.business_id','shop_product.brand','shop_product.spec','shop_product.sort')
            ->join('shop_business','shop_business.business_id','=','shop_product.business_id')
            ->join('shop_brand','shop_brand.brand_id','=','shop_product.brand')
            ->join('specificate','specificate.s_id','=','shop_product.spec')
            ->join('category','category.c_id','=','shop_product.sort')
            ->where('shop_product.is_show',1)
            ->orderby('shop_product.created','desc')
            ->paginate(15);
    }

    /**
     * 关键字搜索
     * @param $search
     * @return mixed
     */
    public function YptShopListSearch($keywords,$stock,$mix_price,$max_price)
    {

            $db =  DB::table($this->table);
            if(!empty($stock)){
                $db->where('stock','>=',$stock);
            }
            if(!empty($mix_price)&&!empty($max_price)){
                $db->whereBetween('shop_product.price',array($mix_price,$max_price));
            }
        $data =   $db
            ->select('product_id','product_name','shop_product.price','category.cate_name','shop_brand.brand_name','specificate.spec_name','business_name','stock','shop_product.created','shop_product.is_show','img1','place','shop_product.business_id','shop_product.brand','shop_product.spec','shop_product.sort')
            ->join('shop_business','shop_business.business_id','=','shop_product.business_id')
            ->join('shop_brand','shop_brand.brand_id','=','shop_product.brand')
            ->join('specificate','specificate.s_id','=','shop_product.spec')
            ->join('category','category.c_id','=','shop_product.sort')
            ->where('shop_product.is_show',1)
            ->where('shop_product.product_name', 'like', "%{$keywords}%")
//            ->orwhere('business_name', 'like', "%{$keywords}%")
//            ->orwhere('shop_brand.brand_name', 'like', "%{$keywords}%")
//            ->orwhere('category.cate_name', 'like', "%{$keywords}%")
            ->orderby('shop_product.created','desc')
            ->paginate(5);
        return  [
            'data'      => $data,
            'keywords'  => $keywords,
            'stock'     => $stock,
            'mix_price' => $mix_price,
            'max_price' => $max_price,
        ];

    }

    /**
     * 排序
     * @param $keywords
     * @param $id
     * @return array
     */
    public function YptShopListOrderSearch($keywords,$id,$stock,$mix_price,$max_price)
    {

        $db =  DB::table($this->table);

        if(!empty($stock)){
            $db->where('stock','>=',$stock);
        }
        if(!empty($mix_price)&&!empty($max_price)){
            $db->whereBetween('shop_product.price',array($mix_price,$max_price));
        }

        if ($id == 'p_up'){
            $db->orderby('price','asc');
            $id ='p_up';
        }
        if ($id == 'p_down'){
            $db->orderby('price','desc');
            $id ='p_down';
        }

        $data = $db
            ->select('product_id','product_name','shop_product.price','category.cate_name','shop_brand.brand_name','specificate.spec_name','business_name','stock','shop_product.created','shop_product.is_show','img1','place','shop_product.business_id','shop_product.brand','shop_product.spec','shop_product.sort')
            ->join('shop_business','shop_business.business_id','=','shop_product.business_id')
            ->join('shop_brand','shop_brand.brand_id','=','shop_product.brand')
            ->join('specificate','specificate.s_id','=','shop_product.spec')
            ->join('category','category.c_id','=','shop_product.sort')
            ->where('shop_product.is_show',1)
            ->where('shop_product.product_name', 'like', "%{$keywords}%")
//            ->orwhere('business_name', 'like', "%{$keywords}%")
//            ->orwhere('shop_brand.brand_name', 'like', "%{$keywords}%")
//            ->orwhere('category.cate_name', 'like', "%{$keywords}%")
            ->where('shop_product.is_show',1)
            ->orderby('shop_product.created','desc')
            ->paginate(5);

        return    [
            'data'      => $data,
            'id'        => $id,
            'keywords'  => $keywords,
            'stock'     => $stock,
            'mix_price' => $mix_price,
            'max_price' => $max_price,
        ];

    }

    /**
     * 商品列表排序
     * @param $id
     * @return array
     */
    public function YptShopListOrder($id,$stock,$mix_price,$max_price)
    {
        $db =  DB::table($this->table);

        if(!empty($stock)){
            $db->where('stock','>=',$stock);
        }
        if(!empty($mix_price)&&!empty($max_price)){
            $db->whereBetween('shop_product.price',array($mix_price,$max_price));
        }
        if ($id == 'p_up'){
            $db->orderby('price','asc');
            $id ='p_up';
        }
        if ($id == 'p_down'){
            $db->orderby('price','desc');
            $id ='p_down';
        }

        $data = $db
            ->select('product_id','product_name','shop_product.price','category.cate_name','shop_brand.brand_name','specificate.spec_name','business_name','stock','shop_product.created','shop_product.is_show','img1','place','shop_product.business_id','shop_product.brand','shop_product.spec','shop_product.sort')
            ->join('shop_business','shop_business.business_id','=','shop_product.business_id')
            ->join('shop_brand','shop_brand.brand_id','=','shop_product.brand')
            ->join('specificate','specificate.s_id','=','shop_product.spec')
            ->join('category','category.c_id','=','shop_product.sort')
            ->where('shop_product.is_show',1)
            ->paginate(5);

        return [
            'data'      => $data,
            'id'        => $id,
            'stock'     => $stock,
            'mix_price' => $mix_price,
            'max_price' => $max_price,
        ];
    }

    /**
     * 分类
     * @param $id
     * @param $c_id
     * @return array
     */
    public function YptShopListOrderSortSearch($c_id,$id,$stock,$mix_price,$max_price)
    {

        $c_id =explode(',',$c_id);
        $db =  DB::table($this->table);

        if(!empty($stock)){
            $db->where('stock','>=',$stock);
        }
        if(!empty($mix_price)&&!empty($max_price)){
            $db->whereBetween('shop_product.price',array($mix_price,$max_price));
        }
        if ($id == 'p_up'){
            $db->orderby('price','asc');
            $id ='p_up';
        }
        if ($id == 'p_down'){
            $db->orderby('price','desc');
            $id ='p_down';
        }
;
        $data = $db
            ->select('product_id','product_name','shop_product.price','category.cate_name','shop_brand.brand_name','specificate.spec_name','business_name','stock','shop_product.created','shop_product.is_show','img1','place','shop_product.business_id','shop_product.brand','shop_product.spec','shop_product.sort')
            ->join('shop_business','shop_business.business_id','=','shop_product.business_id')
            ->join('shop_brand','shop_brand.brand_id','=','shop_product.brand')
            ->join('specificate','specificate.s_id','=','shop_product.spec')
            ->join('category','category.c_id','=','shop_product.sort')
            ->where('shop_product.is_show',1)
            ->whereIn('sort',$c_id)
            ->paginate(5);

        return [
            'data'      => $data,
            'id'        => $id,
            'stock'     => $stock,
            'mix_price' => $mix_price,
            'max_price' => $max_price,
        ];
    }

    public function YptShopListSortSearch($c_id,$stock,$mix_price,$max_price)
    {
        $c_id =explode(',',$c_id);
        $db =  DB::table($this->table);
        if(!empty($stock)){
            $db->where('stock','>=',$stock);
        }
        if(!empty($mix_price)&&!empty($max_price)){
            $db->whereBetween('shop_product.price',array($mix_price,$max_price));
        }
        $data = $db
            ->select('product_id','product_name','shop_product.price','category.cate_name','shop_brand.brand_name','specificate.spec_name','business_name','stock','shop_product.created','shop_product.is_show','img1','place','shop_product.business_id','shop_product.brand','shop_product.spec','shop_product.sort')
            ->join('shop_business','shop_business.business_id','=','shop_product.business_id')
            ->join('shop_brand','shop_brand.brand_id','=','shop_product.brand')
            ->join('specificate','specificate.s_id','=','shop_product.spec')
            ->join('category','category.c_id','=','shop_product.sort')
            ->where('shop_product.is_show',1)
            ->whereIn('sort',$c_id)
            ->paginate(5);

        return [
            'data'      => $data,
            'stock'     => $stock,
            'mix_price' => $mix_price,
            'max_price' => $max_price,
        ];
    }






    /**
     * 分类查询
     * @param $business_id
     * @return mixed
     */
    public function YptShopSort($business_id)
    {
        return  DB::table($this->table)
            ->select('c_id','cate_name')
            ->join('shop_business','shop_business.business_id','=','shop_product.business_id')
            ->join('category','category.c_id','=','shop_product.sort')
            ->where('shop_product.business_id',$business_id)
            ->get();

    }

    public function YptShopBusiness($c_id)
    {
        $c_id =explode(',',$c_id);
        $db =  DB::table($this->table);
        $data = $db
            ->select('business_name','shop_business.created','shop_product.is_show','shop_product.business_id','shop_product.brand','bus_img','logo')
            ->join('shop_business','shop_business.business_id','=','shop_product.business_id')
            ->join('shop_brand','shop_brand.brand_id','=','shop_product.brand')
            ->join('specificate','specificate.s_id','=','shop_product.spec')
            ->join('category','category.c_id','=','shop_product.sort')
            ->where('shop_product.is_show',1)
            ->whereIn('sort',$c_id)
            ->get(12);

        return $data;

    }

    /**
     * 商品详情
     * @param $product_id
     * @return mixed.
     */
    public function YptShopDetails($product_id)
    {
        return  DB::table($this->table)
            ->select('product_id','product_name','brand_name','img1','img2','img3','sort','brand','stock')
            ->where('shop_product.product_id',$product_id)
            ->join('shop_brand','shop_brand.brand_id','=','shop_product.brand')
            ->first();
    }

    public function YptShopDetailsSpec($sort_id)
    {
        return  DB::table($this->table)
            ->select('specificate.spec_name','s_id','specificate.price')
            ->join('specificate','specificate.fid','=','shop_product.sort')
            ->where('shop_product.sort',$sort_id)
            ->get();
    }

}